package com.lf.data.check;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.regex.Pattern;

public class Check {

	protected String id = "\\w{24}";
	protected String updateName = "\\w+";

	public boolean check(String pat, String str) {
		boolean rs = false;
		try {
			if (str != null)
				rs = Pattern.matches(pat, str);
		} catch (Exception e) {
		}
		return rs;
	}

	public boolean checkId(String id) {
		return check(this.id, id);
	}

	public boolean checkIds(List<String> ids) {
		for (String id : ids) {
			if (!checkId(id))
				return false;
		}
		return true;
	}

	public boolean checkUpdateName(String updateName) {
		return check(this.updateName, updateName);
	}

	public String getId() {
		return id;
	}

	public boolean doCheck(Object obj, String name, String value)
			throws NoSuchMethodException, SecurityException,
			IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		Class objClass = obj.getClass();
		String methodName = "check" + name.substring(0, 1).toUpperCase()
				+ name.substring(1);
		Method method = objClass.getMethod(methodName, String.class);
		boolean rs = (boolean) method.invoke(obj, value);
		return rs;
	}
}
